package com.guinea.system.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.guinea.core.utils.StringUtils;
import com.guinea.system.entity.ResRoleRel;
import com.guinea.system.form.ResRoleRelForm;
import com.guinea.core.service.EntityService;
import com.guinea.core.web.StrutsEntityAction;
import com.guinea.system.service.ResRoleRelService;
import com.guinea.system.entity.Role;
import com.guinea.system.service.RoleService;
import com.guinea.system.entity.Resource;
import com.guinea.system.service.ResourceService;

public class ResRoleRelAction extends StrutsEntityAction {
	private ResRoleRelService resRoleRelService;

	public EntityService getEntityService() {
		return resRoleRelService;
	}

	public ResRoleRelService getResRoleRelService() {
		return resRoleRelService;
	}

	public void setResRoleRelService(ResRoleRelService resRoleRelService) {
		this.resRoleRelService = resRoleRelService;
	}
	
	private RoleService roleService;

	private Role role;

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public ActionForward cannotMissRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		role = null;

		String roleIdString = request.getParameter("roleId");
		if (StringUtils.isEmpty(roleIdString)) {
			saveError(request, "parameter.missing");
			return mapping.findForward(LIST);
		}
		Long roleId = new Long(roleIdString);

		role = getRoleService().getRole(roleId);
		if (role == null) {
			saveError(request, "entity.missing");
			return mapping.findForward(LIST);
		}
		return null;
	}
	
	private ResourceService resourceService;

	private Resource resource;

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public ActionForward cannotMissResource(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		resource = null;

		String resourceIdString = request.getParameter("resourceId");
		if (StringUtils.isEmpty(resourceIdString)) {
			saveError(request, "parameter.missing");
			return mapping.findForward(LIST);
		}
		Long resourceId = new Long(resourceIdString);

		resource = getResourceService().getResource(resourceId);
		if (resource == null) {
			saveError(request, "entity.missing");
			return mapping.findForward(LIST);
		}
		return null;
	}

	/**
	 * 显示Form表单时的回调函数.为Form对象添加更多属性,在子类重载.
	 */
	public void editRender(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ResRoleRelForm resRoleRelForm = (ResRoleRelForm) form;
		ResRoleRel resRoleRel = (ResRoleRel) entity;
		
		if (entity != null) 
			resRoleRelForm.setRoleId(resRoleRel.getRole().getId());
		request.setAttribute("allRole", getRoleService().get());
		if (entity != null) 
			resRoleRelForm.setResourceId(resRoleRel.getResource().getId());
		request.setAttribute("allResource", getResourceService().get());
	}

	public ActionForward saveValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward actionForward = super.saveValidator(mapping, form,
				request, response);
		
		if (actionForward == null)
			actionForward = cannotMissRole(mapping, form, request, response);
		if (actionForward == null)
			actionForward = cannotMissResource(mapping, form, request, response);
		
		if(entity == null && getResRoleRelService().getByRoleResource(role.getId(), resource.getId()).size() > 0){
			saveError(request, "entity.duplicated");
			return mapping.findForward(LIST);
		}

		return actionForward;
	}

	/**
	 * 保存业务对象的函数.
	 */
	protected void doSaveEntity(HttpServletRequest request) {
		ResRoleRel resRoleRel = (ResRoleRel) entity;
		
		resRoleRel.setRole(role);
		resRoleRel.setResource(resource);
		
		entity = resRoleRel;
		super.doSaveEntity(request);
	}

}
